package com.example.rocketmqproducer.producer;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.stereotype.Component;

/**
 * @author sherry
 * @description
 * @date Create in 2019/12/23
 * @modified By:
 */

@Component
public class PayProducer {

    private String produceGroup = "pay_group";
    private String nameServerAdrr = "ROCKET_HOST_NAME_SERVER_ADDR:9876";
    private DefaultMQProducer defaultMQProducer;

    public PayProducer() {
        defaultMQProducer = new DefaultMQProducer(produceGroup);
//        如果是多节点的话，多个节点之间是有冒号分割，如：  "192.168.3.132:9876;192.168.3.132:9877;192.168.3.132:9878"
        defaultMQProducer.setNamesrvAddr(nameServerAdrr);
        start();
    }

    public DefaultMQProducer getProducer() {
        return this.defaultMQProducer;
    }

    /**
     * 对象在使用前，必须要调用一次
     * 只能初始化一次
     */
    public void start() {
        try {
            this.defaultMQProducer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 一般在应用的上下文，使用上下文监听器，进行关系
     */
    public void shutdown() {
        this.defaultMQProducer.shutdown();
    }
}